package io.xxx.xbutler.service;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xxx.xbutler.data.InvokeMapper;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.data.RoomMapper;
import io.xxx.xbutler.domain.*;
import io.xxx.xbutler.sdk.TanJingClient;
import io.xxx.xbutler.sdk.request.SetAliasForGroupRequest;
import io.xxx.xbutler.sdk.response.SetAliasForGroupResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class RoomService extends ServiceImpl<RoomMapper, Room> {

    @Resource
    private TanJingClient tanJingClient;

    @Resource
    private RobotMapper robotMapper;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private InvokeMapper invokeMapper;

    @Resource
    private ExportTaskService exportTaskService;

    public Page<Room> list(Room room, int pageNumber, int pageSize) {
        LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
        if (room.getId() != null) {
            wrapper.eq(Room::getId, room.getId());
        }
        if (StringUtils.hasText(room.getName())) {
            wrapper.like(Room::getName, room.getName());
        }
        return roomMapper.selectPage(new Page<>(pageNumber, pageSize), wrapper);
    }

    public Room get(String id) {
        return roomMapper.selectById(id);
    }

    public Long export(Room room, User user) {
        return exportTaskService.create(ExportTaskType.ROOM_LIST, room, user);
    }

    public String setRobotAlias(String roomId, String robotId, String alias) {
        Room room = roomMapper.selectById(roomId);
        if (room == null) {
            throw new IllegalArgumentException("群信息不存在[" + roomId + "]");
        }
        Robot robot = robotMapper.selectById(robotId);
        if (robot == null) {
            throw new IllegalArgumentException("机器人信息不存在[" + robotId + "]");
        }

        SetAliasForGroupRequest request = new SetAliasForGroupRequest()
                .setChatRoomSerialNo(room.getSerialNo())
                .setRobotSerialNo(robot.getSerialNo())
                .setAlias(alias);
        SetAliasForGroupResponse response = tanJingClient.execute(request); // 需要收到回调才能确定修改昵称是否成功
        if (!response.isSuccess()) {
            log.error("修改机器人的群内昵称失败[request:{}, response:{}]", JSON.toJSONString(request), JSON.toJSONString(response));
        }
        Invoke r = new Invoke()
                .setSerialNo(response.getSerialNo())
                .setRobotId(robotId)
                .setRobotSerialNo(robot.getSerialNo())
                .setParams(JSON.parseObject(JSON.toJSONString(request.params())));
        invokeMapper.insert(r);
        return response.getSerialNo();
    }
}
